<?php

class OperatorApp extends BackendApp
{
    var $_exception_mod;
	var $_operator_mod;
    var $_exattr_mod;
	function __construct()
	{
		$this->OperatorApp();
	}
	function OperatorApp()
	{
		parent::BackendApp();

		$this->_exception_mod =& m('exception');
	    $this->_operator_mod =& m('operator');
	    $this->_exattr_mod =& m('exattr');
	}

	/* 管理 */
	function index()
	{
		$wm_carrier=empty($_GET['wm_carrier'])?'':$_GET['wm_carrier'];

		
		
		$conditions = "1=1 AND o_name LIKE '%".$wm_carrier."%'";
		
		
			$sort  = 'o_tid';
			$order = 'desc';
		
	       	$page   =   $this->_get_page(10);   //获取分页信息
			$operator=$this->_operator_mod->find(array(
	        'fields'   => 'this.*',
			 'conditions'  => $conditions,
			'limit'   => $page['limit'],
			'order'   => "$sort $order", //必须加别名
			 'count'   => true   //允许统计
			));
		   $page['item_count']=$this->_operator_mod->getCount();   //获取统计数据

		   
		   $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		   
		   
		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('operator', $operator);

		$this->assign('tabself', json_encode(array('text'=>'运营商管理')));
        $this->display('operator.index.html');
	}
 //添加
	function add()
	{
		if (!IS_POST)
		{
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->assign('tabself', json_encode(array('text'=>'新建运营商')));
            $this->assign('tabback', json_encode(array('text'=>'运营商管理','url'=>'')));
						
			$this->display('operator.form.html');
		}
		else
		{
			
			
		
			$data = array(
			
				'o_code' => $_POST['o_code'],
                'o_name' => $_POST['o_name'],
                'o_linkman'  => $_POST['o_linkman'],
                'o_linktel'  => $_POST['o_linktel'],
			    'o_address'  => $_POST['o_address'],
			    'o_carrier' => $_POST['o_carrier'],
			    'o_consignor' => $_POST['o_consignor'],
			);

		/* 检查名称是否已存在 */
			if ($this->_operator_mod->unique(trim($data['o_code']), trim($data['o_name']),'') == 2)
			{
				$this->show_warning('编码有重复');
				return;
			}elseif ($this->_operator_mod->unique(trim($data['o_code']), trim($data['o_name']),'') == 1) {
				$this->show_warning('名称有重复');
				return;;
			}
			
			
			if (!$id = $this->_operator_mod->add($data))
			{
				$this->show_warning('添加失败','', 'index.php?app=operator&act=add');
				return;
			}
			$this->show_message('add_successed','返回运营商',    'index.php?app=operator&act=edit&id='.$id
                );
		}
	}



	//删除咨询
	function drop()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);

		if (!$this->_operator_mod->drop($arrs))
		{
			$this->show_warning($this->_operator_mod->get_error());

			return;
		}
		$this->show_message('删除成功');
			
	}
	

	//编辑
	function edit()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST)
		{

			$operator=$this->_operator_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'o_tid='.$id,
			));

			$this->assign('id', $id);

			$this->assign('operator', $operator);

	        $this->assign('tabself', json_encode(array('text'=>'编辑运营商')));
            $this->assign('tabback', json_encode(array('text'=>'运营商管理','url'=>'')));
			$this->display('operator.form.html');
		}else {
			$data = array(
			'o_code' => $_POST['o_code'],
			'o_name'  => $_POST['o_name'],
			'o_linkman'  => $_POST['o_linkman'],
			'o_linktel' => $_POST['o_linktel'],
			'o_address' => $_POST['o_address'],
			'o_carrier' => $_POST['o_carrier'],
			'o_consignor' => $_POST['o_consignor'],
			);
			/* 检查名称是否已存在 */
			if ($this->_operator_mod->unique(trim($data['o_code']), trim($data['o_name']),$id)==2)
			{
				$this->show_warning('编码有重复','', 'index.php?app=operator&act=edit&id=' . $id);
				return;
			}elseif ($this->_operator_mod->unique(trim($data['o_code']), trim($data['o_name']),$id) == 1) {
				$this->show_warning('名称有重复','', 'index.php?app=operator&act=edit&id=' . $id);
				return;
			}
            /* 保存 */
            $rows = $this->_operator_mod->edit($id, $data);
            if ($this->_operator_mod->has_error())
            {
                $this->show_warning($this->_operator_mod->get_error(),'', 'index.php?app=operator&act=edit&id=' . $id);
                return;
            }
            $this->show_message('edit_ok','edit_again',   'index.php?app=operator&act=edit&id=' . $id);
		}
	}
	

	//异步修改数据

	function ajax_col()
	{




		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
	
		$data   = array();

		if (in_array($column ,array('o_carrier','o_consignor')))
		{
			$data[$column] = $value;

			$this->_operator_mod->edit($id, $data);
			if(!$this->_operator_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
